﻿package com.devilcat.ob
{
	//////////////////Status Use Move/ReText
	import flash.display.MovieClip;
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	import fl.transitions.TweenEvent;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.display.Loader;
	import flash.net.URLRequest;
    import flash.utils.*;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	import com.devilcat.dt.Gamedt;
	import com.devilcat.dt.Pokemondt;
	import com.devilcat.dt.Movedt;
	import com.devilcat.dt.Itemdt;
	import com.devilcat.ui.PokemonInfor;
	import com.devilcat.ui.FloatingText;
	import com.devilcat.ui.BlankButtonObject;

	public class PVPPokemonob extends MovieClip
	{
		const BUG = 0;
		const DARK = 1;
		const DRAGON = 2;
		const ELECTRIC = 3;
		const FIGHTING = 4;
		const FIRE = 5;
		const FLYING = 6;
		const GHOST = 7;
		const GRASS = 8;
		const GROUND = 9;
		const ICE = 10;
		const NORMAL = 11;
		const POISON = 12;
		const PSYCHIC = 13;
		const ROCK = 14;
		const STEEL = 15;
		const WATER = 16;
		const NONE = 17;
		private var tweenList:Array;
		public var pokemon:Pokemondt;
		public var Target:MovieClip;
		public var autoAttack:uint;
		public var autoReload:uint;
		public var autoTargetReload:uint;
		public var timerBar:Tween;
		public var timerBarDelay:int=3;
		public var IsAI:Boolean=false;
		//public var OldHP:Number;
		public var Attack:Number;
		public var Defense:Number;
		public var SpAtk:Number;
		public var SpDef:Number;
		public var Speed:Number;
		public var Accuracy:Number;
		public var Evasive:Number;
		public var Flinch:Boolean;
		public var UsingItem:Boolean=false;
		public var CurrentMove:Movedt;
		public var CurrentDamage:Number=0;
		public var AttackTurn:Number=0;
		public var AttackTurnBonus:Number=0;
		public var AttackFunction:String="";
		public var AIMoveName:String="";
		var PVPTurn:uint=0;
		var countFloat:int=0;

		public function PVPPokemonob(_pokemon:Pokemondt)
		{
			pokemon = _pokemon;
			pokemon.SetAttackMC(this);
			SetDefaultStat();
			freeTween();
			Flinch = false
			MovePanel.visible=false;			
			mcAtk.alpha=0;
			UsingItem=false;
		}
		public function InitPokemon(_direction:Boolean=false)
		{
			RegeneratorAbility();
			mcHPBar.scaleX = pokemon.CurrentHP / pokemon.HP;
			ReText();		
			var imageLoader:Loader = new Loader();
			var imageLink:URLRequest = new URLRequest("http://dex.poke-mega.org/image/pokemon/"+pokemon.pid+".png");
			imageLoader.load(imageLink);
			imageLoader.contentLoaderInfo.addEventListener(Event.INIT,imageLoaded);
			function imageLoaded(e:Event):void
			{
				mcImage.addChild(imageLoader);
				if(_direction){
					mcImage.x = 75;
					mcImage.scaleX = -1;
					LoadMoves();
					MovePanel.visible=false;
					addEventListener(MouseEvent.MOUSE_OVER, function() {MovePanel.visible=true});
					addEventListener(MouseEvent.MOUSE_OUT, function() {MovePanel.visible=false});
				}else{					
					mcAtk.scaleX = -1;
					mcAtk.x = 60
				}
			}
			//-----------------------------------------
			if (pokemon.Ability == "Intimidate")
			{
				Target.IntimidateAbility();
			}
			//SendSQL();
			//autoReload = setInterval(LoadSQL, 200);	
		}//MovePanel
		public function CalculateTime(Move:Movedt):Number{
			return 1000;
		}
		public function ClearAuto(){			
			clearInterval(autoAttack);
//SQLData.mainClip.Trace("ClearAuto");
		}
		public function EnableAutoAttack(_mode:String){	
		countFloat=0;
			if(AttackTurn==0 && !UsingItem){		
			ClearAuto();
				switch (_mode){
					case "Move1":
					case "Move2":
					case "Move3":
					case "Move4":
						CurrentMove=pokemon[_mode];
						autoAttack = setInterval(UseMove, 2000);		
						timerBar=new Tween (mcTimer,"scaleX",Regular.easeOut,0,1,timerBarDelay,true);						
						break;
				}
			}
		}
		public function DelayUseItem(itemDelayTime:uint){			
			ClearAuto();
			UsingItem=true;	
			timerBar=new Tween (mcTimer,"scaleX",Regular.easeOut,0,1,itemDelayTime+2,true);				
			timerBar.addEventListener(TweenEvent.MOTION_FINISH,EndUseItem);							
		}
		public function ReText(){
			txtName.text = pokemon.Name + " " + pokemon.Status;
			txtHP.text=pokemon.CurrentHP+"";			
			tweenList.unshift(new Tween (mcHPBar,"scaleX",Strong.easeOut,mcHPBar.scaleX,(pokemon.CurrentHP)/pokemon.HP,2,true));				
		}
		public function EndUseItem(e:Event){			
			UsingItem=false;
		}
		private function pokemonGetExp(_Target:Pokemondt){
			var countx:Number=0;
			if(pokemon.Total()-_Target.Total()<150){
				if (_Target.Exp.HP>0) {
					addChild(new FloatingText("+ "+_Target.Exp.HP+" HP",0,-countx*15,2));
					countx++;
				}
				if (_Target.Exp.AT>0) {
					addChild(new FloatingText("+ "+_Target.Exp.AT+" Attack",0,-countx*15,2));
					countx++;
				}
				if (_Target.Exp.DF>0) {
					addChild(new FloatingText("+ "+_Target.Exp.DF+" Defense",0,-countx*15,2));
					countx++;
				}
				if (_Target.Exp.SA>0) {
					addChild(new FloatingText("+ "+_Target.Exp.SA+" SpAtk",0,-countx*15,2));
					countx++;
				}
				if (_Target.Exp.SD>0) {
					addChild(new FloatingText("+ "+_Target.Exp.SD+" SpDef",0,-countx*15,2));
					countx++;
				}
				if (_Target.Exp.SP>0) {
					addChild(new FloatingText("+ "+_Target.Exp.SP+" Speed",0,-countx*15,2));
					countx++;
				}
				pokemon.getEXP(_Target.Exp.HP,_Target.Exp.AT,_Target.Exp.DF,_Target.Exp.SA,_Target.Exp.SD,_Target.Exp.SP);
			}else{				
				this.parent.addChild(new FloatingText("No Exp",0,-countx*15,2));
			}
		}	
		public function SetDefaultStat(){
			Attack = pokemon.Attack;
			Defense = pokemon.Defense;
			SpAtk = pokemon.SpAtk;
			SpDef = pokemon.SpDef;
			Speed = pokemon.Speed;							
			var Query:String="UPDATE twalk_pokemon SET CurrentHP='"+pokemon.HP+"' ,Status='"+""+"' ,TemAttack='"+Attack+"' ,TemDefense='"+Defense+"' ,TemSpAtk='"+SpAtk+"' ,TemSpDef='"+SpDef+"' ,TemSpeed='"+Speed+"' ,MoveAttack='"+""+"' ,PVPTurn='"+"0"+"' WHERE  upid='"+pokemon.upid+"'";
			//SQLData.SendQuery(Query);
		}
		public function TraceStats(){
			
		}
		public function setTarget(_Target:MovieClip)
		{
			Target = _Target;
		}
		private function freeTween()
		{
			tweenList = null;
			tweenList=new Array();
		}

		public function LoadMoves() {
			MovePanel.visible=true;
			for (var i:int=1; i<5; i++) {
				var MoveButton:BlankButtonObject=new BlankButtonObject(pokemon["Move"+i].PP+"MP",pokemon["Move"+i].Name);
				MoveButton.SetData(i);
				MoveButton.y=(i-1)*24;
				MovePanel.addChild(MoveButton);
			}			
			MovePanel.addEventListener(MouseEvent.CLICK,DoMoveEvent);
		}
		private function DoMoveEvent(evt:MouseEvent) {
			EnableAutoAttack("Move"+evt.target.bid);
			
		}
		public function EnableMoves() {
			MovePanel.visible=true;
		}
		public function DisableMoves() {
			MovePanel.visible=false;
		}
		public function ReceiveDamage(_damage:Number=0){
			if(_damage>0){
				addChild(new FloatingText(_damage+"",0,0));
				countFloat++;
			}						
			if(pokemon.CurrentHP<_damage)_damage=pokemon.CurrentHP;
			pokemon.CurrentHP-=_damage;		
			ReText();		
			
			if(pokemon.CurrentHP==0){			
				RemoveMeTween();
				Gamedt.BattleUI.PokemonFailed(IsAI);
			}						
			pokemon.SetCorrectHP();
			if(IsAI)Gamedt.updatePokemonPVP.Update(pokemon);
		}
		public function RemoveMeTween(){			
			tweenList.unshift(new Tween (this,"alpha",Strong.easeOut,1,0,2,true));		
			tweenList[0].addEventListener(TweenEvent.MOTION_FINISH,RemoveMeEvent);	
		}
		public function RemoveMe(){					
			if(this.parent != null){
				ClearAuto();
				pokemon.AttackMC=null;
				this.parent.removeChild(this);
			}
		}
		public function RemoveMeEvent(e:Event){
			RemoveMe();			
		}
		////////////////////////////////////Abilitis

		public function IntimidateAbility()
		{
			if (pokemon.Ability != "ClearBody" && pokemon.Ability != "HyperCutter" && pokemon.Ability != "WhiteSmoke")
			{
				SetStat("Attack",pokemon.Attack*0.8);
			}
		}
		public function SetStat(_stat:String, _value:Number) {
			this[_stat]=Math.round(_value);
		}
		private function RegeneratorAbility()
		{
			if (pokemon.Ability == "Regenerator")
			{
				pokemon.CurrentHP +=  Math.round(pokemon.HP / 3);
				
				pokemon.SetCorrectHP();
				if(IsAI)Gamedt.updatePokemonPVP.Update(pokemon);
			}
		}
		
		public function Damage(Move:Movedt):Number{			
		Target.TraceStats()
			var Stab:Number = 1;
			var PhysicalOrSpecial:Number = 0;
			var typeX:Number = Gamedt.typeChart[this[Move.Type]][this[Target.pokemon.Type1]] * Gamedt.typeChart[this[Move.Type]][this[Target.pokemon.Type2]];
			var damageReturn:Number;
			if (pokemon.Type1 == Move.Type || pokemon.Type2 == Move.Type)
			{
				Stab = 1.5;
			}
			if (Move.Category == "Physical")
			{
				PhysicalOrSpecial = Attack / Target.pokemon.Defense;
			}
			else
			{
				PhysicalOrSpecial = SpAtk / Target.pokemon.SpDef;
			}
			
			damageReturn=Math.round(PhysicalOrSpecial*(Stab*typeX*Move.Attack+Math.round(Math.random()*20*typeX))/4);
			if (Target.pokemon.Ability == "Filter" && typeX > 1)
			{
				damageReturn *=  3 / 4;
			}		
			//-------------------MOVE DAMAGE ADD BY NAME
			switch (Move.Name)
			{
				case "GyroBall" :
				case "ElectroBall" :
					damageReturn+=Math.round(10*(pokemon.Speed/Target.pokemon.Speed));
					break;
				case "CrushGrip" :
					damageReturn+=Math.round(10*(Target.pokemon.HP/Target.pokemon.CurrentHP));
					break;
				case "Wake-upSlap" :
					if (Target.pokemon.Status == "Sleep")
					{
						damageReturn *=  2;
						Target.pokemon.RemoveStatus();
					}
					break;
				case "Brine" :
					if (Target.pokemon.CurrentHP < Target.pokemon.HP / 2)
					{
						damageReturn *=  2;
					}
					break;
				case "MetalBurst" :
					damageReturn = Math.round(Target.pokemon.Attack * 1.5);
					break;
				case "Reversal" :
				case "Flail" :
					damageReturn+=Math.round(10*((pokemon.HP-pokemon.CurrentHP)/20));
					break;
				case "SuperFang" :
					damageReturn = Math.round(Target.pokemon.CurrentHP / 2);
					break;
				case "WringOut" :
				case "WaterSpout" :
					damageReturn+= -Math.round(10*((pokemon.HP-pokemon.CurrentHP)/20));
					break;
				case "FinalGambit" :
					damageReturn = Target.pokemon.CurrentHP;
					pokemon.CurrentHP = 0;
					break;
			}
			//-------------------MOVE SECOND EFFECT
			if (Move.isCritical() && Target.pokemon.Ability != "ShellArmor")
			{
				damageReturn *=  2;
			}
			if (String(Move.Effect.XD)!="")
			{
				var rXD:Number = Math.round(Math.random() * (Number(Move.Effect.XD) - 1)) + 1;
				damageReturn *=  rXD;
			}
			if (String(Move.Effect.KO)!="")
			{
				damageReturn = 99999;
			}
			if (String(Move.Effect.CHP)!="")
			{
				damageReturn = Number(Move.Effect.CHP);
			}
			if (String(Move.Effect.D0)!="")
			{
				damageReturn = 0;
			}
			if (String(Move.Effect.SD)!="")
			{
				pokemon.CurrentHP = 0;
				if (Target.pokemon.Ability == "Damp")
				{
					damageReturn = 0;
				}
			}		
			//-------------------POKEMON ATTACK ABILITIS
			if (pokemon.CurrentHP < pokemon.HP/3) {
				switch (pokemon.Ability) {
					case "Overgrow" :
						if (Move.Type=="GRASS") {
							damageReturn=Math.round(damageReturn*1.5);
						}
						break;
					case "Blaze" :
						if (Move.Type=="FIRE") {
							damageReturn=Math.round(damageReturn*1.5);
						}
						break;
					case "Torrent" :
						if (Move.Type=="WATER") {
							damageReturn=Math.round(damageReturn*1.5);
						}
						break;
					case "Swarm" :
						if (Move.Type=="BUG") {
							damageReturn=Math.round(damageReturn*1.5);
						}
						break;
				}
			}
					
					//-------------------POKEMON DEFENSIVE ABILITIS
			switch (Target.pokemon.Ability) {
				case "EffectSpore" :
					if (Move.Category=="Physical" && pokemon.Status=="") {
						if (NotMiss(90)) {
							var EffectSpore:Number=Math.round(Math.random()*2);
							switch (EffectSpore) {
			case 0 :
				MoveStatusEffect("Poison",100)
				break;
			case 1 :
				MoveStatusEffect("Sleep",100)
				break;
			case 2 :
				MoveStatusEffect("Parlyz",100)
				break;
							}
						}
					}
					break;
				case "Justified" :
					if (Move.Type=="DARK") {
						Target.SetStat("Attack",Target.pokemon.Attack*1.1);
					}
					break;
				case "Heatproof" :
					if (Move.Type=="FIRE") {
						damageReturn=Math.round(damageReturn/2);
					}
					break;
				case "Levitate" :
					if (Move.Type=="GROUND" || Move.Name=="SkyDrop"|| Move.Name=="Spikes") {
						damageReturn*=0;
					}
					break;
				case "ThickFat" :
					if (Move.Type=="FIRE" || Move.Type=="ICE") {
						damageReturn=Math.round(damageReturn/2);
					}
					break;
				
				case "FlashFire" :
					if (Move.Type=="FIRE") {
						damageReturn=0;
						//Check
						Target.pokemon.addBonusMoveAtkByAbility("FIRE",1.5);
					}
					break;
				case "WeakArmor" :
					if (Move.Category=="Physical") {
						Target.SetStat("Defense",Target.pokemon.Defense*0.9);
						Target.SetStat("Speed",Target.pokemon.Speed*1.1);
					}
				case "Lightningrod" :
					if (Move.Type=="FIGHTING") {
						Target.SetStat("SpAtk",Target.pokemon.SpAtk*1.1);
						damageReturn=0;
					}
				case "FlameBody" :
					if (Move.Category=="Physical") {
						SetStatus("Burn");
					}
					break;
				case "Aftermath" :
					if (Move.Category=="Physical") {
						pokemon.CurrentHP-=Math.round(pokemon.HP/4);
					}
				case "WaterAbsorb" :
					if (Move.Type=="WATER") {
						pokemon.CurrentHP-=Math.round(pokemon.HP/4);
					}
					break;
				case "DrySkin" :
					if (Move.Type=="WATER") {
						Target.pokemon.CurrentHP+=Math.round(Target.pokemon.HP/4);
					}
					if (Move.Type=="FIRE") {
						damageReturn=Math.round(damageReturn*1.25);
					}
					break;
			}		
			if(Target.pokemon.Ability=="Sturdy"){
				if(Target.pokemon.CurrentHP==Target.pokemon.HP && damageReturn>=Target.pokemon.HP){
					damageReturn-=1;
				}					
			}
			
			return damageReturn;			
		}
		public function UseMove()
		{
			PVPTurn++;
			countFloat=0;
			if(Target.pokemon.CurrentHP>0 && pokemon.CurrentHP>0 && !UsingItem){
			timerBar=new Tween (mcTimer,"scaleX",Regular.easeOut,0,1,timerBarDelay,true);
			var _Status:Boolean=pokemon.IsStatusEffect();
			if (_Status) {
				ReText();
				switch (pokemon.Status) {
					case "Parlyz" :
					case "Sleep" :
					case "Frozen" :
						addChild(new FloatingText(pokemon.Status+" | Cannot Attack!",0,-countFloat*15,1));
						countFloat++;
						return;
						break;
					case "Confuse" :
						addChild(new FloatingText(pokemon.Status+" | Selft Attack!",0,-countFloat*15,1));
						countFloat++;
						pokemon.SetCorrectHP();
						if(IsAI)Gamedt.updatePokemonPVP.Update(pokemon);
						return;
						break;
					case "Burn" :
					case "Poison" :
						addChild(new FloatingText(pokemon.Status+" | Lost HP!",0,-countFloat*15,1));
						countFloat++;
						pokemon.SetCorrectHP();
						if(IsAI)Gamedt.updatePokemonPVP.Update(pokemon);
						break;
				}
			}
			var Move:Movedt=CurrentMove;
			if(String(Move.Effect.AH)!="" || (NotMiss(Move.Accuracy) && NotMiss(pokemon.Accuracy) && !NotMiss(Target.pokemon.Evasive))){	
			//-------------------MORE TURN
			if(AttackTurn>0){				
				//Check
				AttackTurn--;
				switch(AttackFunction){
					case "T2":					
						break;
					case "MT":
						CurrentDamage*=AttackTurnBonus;
						break
				}		
				
				if (AttackTurn==0) {
					if (AttackFunction=="MT") {
						MoveStatusEffect(String(Move.Effect.MT.N),Number(Move.Effect.MT.A));
					}
					//CurrentMove=null;
					AttackTurnBonus=0;
					AttackFunction="";
				}
			}else{
				if(Move.Attack>1){
					//-------------------BASE DAMAGE					
					CurrentDamage=Damage(Move);
					if (CurrentDamage>1) {
						if (String(Move.Effect.T2)!="") {
							AttackFunction="T2";
							AttackTurn=1;
							CurrentMove=Move;
						} else {
							if (String(Move.Effect.T1)!="") {
								AttackFunction="T1";
								AttackTurn=1;
							}							
						}
						if (String(Move.Effect.MT)!="") {
							CurrentMove=Move;
							AttackTurn=Number(Move.Effect.MT.T)-1;
							AttackTurnBonus=Number(Move.Effect.MT.F);
							AttackFunction="MT";
						}
					}
					//-------------------D E A L D A M A G E
					mcAtk.gotoAndStop(Move.Type);
					tweenList.unshift(new Tween (mcAtk,"alpha",Strong.easeOut,1,0,1,true));
					tweenList.unshift(new Tween (mcAtk,"x",Strong.easeOut,0,Target.x-this.x,1,true));
					Target.ReceiveDamage(CurrentDamage);
					//-------------------EFFECT AFTER DEAL DAMAGE
					if (String(Move.Effect.RC)!="") {
						pokemon.CurrentHP-=Math.round(CurrentDamage*Number(Move.Effect.RC/100));
					}
					if (String(Move.Effect.LS)!="") {
						pokemon.CurrentHP+=Math.round(CurrentDamage*Number(Move.Effect.LS));
					}
					
					if (String(Move.Effect.FL)!="" && Target.pokemon.Ability!="InnerFocus") {
						if (NotMiss(Number(Move.Effect.FL))) {
							Target.Flinch=true;
							//Target.CurrentMove=null;
							Target.AttackTurn=0;
						}
					}
					//-------------------ABILITIS
					switch (pokemon.Ability) {
						case "ShedSkin" :
							if (NotMiss(30)) {
								RemoveStatus();
							}
							break;
						case "Synchronize" ://Chuyển Status cho đối phương
							if (pokemon.Status=="Parlyz" || pokemon.Status=="Burn" || pokemon.Status=="Poison") {
								Target.MoveStatusEffect(pokemon.Status,100);
							}
							break;
					}
				}
				//-------------------END IF DAMAGE
				else//SPECIAL MOVE
				{
//SQLData.mainClip.Trace("SPECIAL MOVE");
					switch(Move.Name){
					case "Aromatherapy":
						RemoveStatus();					
						break;
					case "ChipAway":
					case "ClearSmog":
						Target.SetDefaultStat();					
						break;
					case "Haze":
						SetDefaultStat();
						break;
					case "BellyDrum":
						Attack+=Attack*2;
						pokemon.CurrentHP-=Math.round(pokemon.HP/2);					
						break;
					case "PainSplit":
						var arg:Number=Math.round((pokemon.CurrentHP+Target.pokemon.CurrentHP)/2);		
						Target.pokemon.CurrentHP=arg;				
						pokemon.CurrentHP=arg;
						break;
					case "Endeavor":				
						Target.pokemon.CurrentHP=pokemon.CurrentHP;						
						break;
					default:					
					
					}					
				}//-------------------END SPECIAL
				
				if (String(Move.Effect.S)!="") {
					ChangeMoreStats(Move.Effect.S);
				}
				if (String(Move.Effect.O)!="") {
					if (!(Move.Attack>1 && Target.pokemon.Ability=="ShieldDust")) {
						Target.ChangeMoreStats(Move.Effect.O);
					}
				}
				if (String(Move.Effect.SS)!="") {
					MoveStatusEffect(String(Move.Effect.SS.N),Number(Move.Effect.SS.A));
				}
				if (String(Move.Effect.OS)!="") {
					Target.MoveStatusEffect(String(Move.Effect.OS.N),Number(Move.Effect.OS.A));
				}
				if (String(Move.Effect.WT)!="") {
					//setBattleWeather();
				}
				Target.SendSQL();
				SendSQL();
			}
			}else{
				//CurrentMove=null;
				AttackTurn=0;
				AttackTurnBonus=0;
				AttackFunction="";
				addChild(new FloatingText("Miss!",0,0));
						countFloat++;
			}
			}
		}
		function MoveStatusEffect(_status:String,_value:Number) {
				if (!((pokemon.Ability=="Insomnia"||Target.pokemon.Ability=="Insomnia") && _status=="Poison")) {
					if (NotMiss(_value) && pokemon.Status=="") {
						SetStatus(_status);
					}
				}
			}
		public function RemoveStatus() {
			switch (pokemon.Status) {
					case "Burn" :
						if (pokemon.Ability!="Guts" && pokemon.Ability!="FlareBoost") {
							SpAtk+=Math.round(pokemon.SpAtk/2);
						}
						if (pokemon.Ability=="FlareBoost") {
							SpAtk+=Math.round(pokemon.SpAtk*0.5);
						}
						break;
					case "Parlyz" :
						if (pokemon.Ability!="QuickFeet") {
							Speed+=Math.round(pokemon.Speed/4);
						}
						break;
			}
			if (pokemon.Ability=="Guts" && (pokemon.Status=="Burn" ||pokemon.Status=="Parlyz" ||pokemon.Status=="Poison" ||pokemon.Status=="Sleep")) {
				Attack-=pokemon.Attack;
			}
			pokemon.Status="";
			pokemon.StatusLost=0;
			pokemon.StatusTurn=0;
		}
		function ChangeMoreStats(_listStat:XMLList) {
			if (NotMiss(Number(_listStat.A))) {
				var listStatLength:Number=_listStat.N.length();
				for (var i=0; i<listStatLength; i++) {
					ChangeStats(String(_listStat.N[i]),Number(_listStat.F[i]));
				}
			}
		}
		function ChangeStats(_Stat:String,_value:Number,_isMove:Boolean=true)
		{			
			if(_isMove){
				if (Math.abs(_value)<1) {
					_value=Math.round(_value*pokemon[_Stat]);
				}
				if (pokemon.Ability=="Defiant" && _value<0)
				{
					this[_Stat]+=_value;
					Attack+=5;
				}
				if (pokemon.Ability=="ClearBody" && _value<0) {
					_value = 0;
				}
				if (pokemon.Ability=="BigPecks" && _value<0 && _Stat=="Defense") {
					_value = 0;
				}
				if (pokemon.Ability=="HyperCutter" && _value<0 && _Stat=="Attack") {
					_value = 0;
				}
				if (pokemon.Ability=="KeenEye" && _value<0 && _Stat=="Accuracy") {
					_value = 0;
				}
			}
			if(_Stat=="HP"){
				pokemon.CurrentHP+=_value;				
				pokemon.SetCorrectHP();
				if(IsAI)Gamedt.updatePokemonPVP.Update(pokemon);	
			}else{
				this[_Stat]+=_value;
			}			
			ReText();		
			CheckStat();
		}
		public function CheckStat(){
			if(Attack<1)Attack=1;
			if(Defense<1)Defense=1;
			if(SpAtk<1)SpAtk=1;
			if(SpDef<1)SpDef=1;
			if(Speed<1)Speed=1;			
		}

		public function SetStatusBeforeBattle() {
			switch (pokemon.Status) {
					case "Burn" :
						if (pokemon.Ability!="Guts" && pokemon.Ability!="FlareBoost") {
							SpAtk-=Math.round(pokemon.SpAtk/2);
						}
						if (pokemon.Ability=="FlareBoost") {
							SpAtk+=Math.round(pokemon.SpAtk*0.5);
						}
						break;
					case "Parlyz" :
						if (pokemon.Ability!="QuickFeet") {
							Speed-=Math.round(pokemon.Speed/4);
						}
						break;
			}
			if (pokemon.Ability=="Guts" && (pokemon.Status=="Burn" ||pokemon.Status=="Parlyz" ||pokemon.Status=="Poison" ||pokemon.Status=="Sleep")) {
				Attack+=pokemon.Attack;
			}
			ReText();
		}

		public function SetStatus(_Status:String) {
			if (pokemon.Status=="") {
				switch (_Status) {
					case "Burn" :
						if (pokemon.Type1=="FIRE"||pokemon.Type2=="FIRE" ||pokemon.Ability=="Heatproof") {
							_Status=="";
						} else {
							pokemon.StatusLost=Math.round(pokemon.HP/8);
							if (pokemon.Ability!="Guts") {
								SpAtk-=Math.round(pokemon.SpAtk/2);
							}
						}
						if (pokemon.Ability=="FlareBoost") {
							SpAtk+=Math.round(pokemon.SpAtk*0.5);
						}
						break;
					case "Parlyz" :
						Speed-=Math.round(pokemon.Speed/4);
						break;
					case "Poison" :
						if (pokemon.Type1=="STEEL"||pokemon.Type2=="STEEL" || pokemon.Type1=="POISON"||pokemon.Type2=="POISON" || pokemon.Ability=="Immunity") {
							_Status=="";
						} else {
							pokemon.StatusLost=Math.round(pokemon.HP/8);
						}
						break;
					case "Sleep" :
						pokemon.StatusTurn=Math.round(Math.random()*6)+1;
						if (pokemon.Ability=="EarlyBird") {
							pokemon.StatusTurn=Math.round(pokemon.StatusTurn/2);
						}
						break;
					case "Frozen" :
						if (pokemon.Type1=="ICE"||pokemon.Type2=="ICE") {
							_Status=="";
						}
						break;
					case "Confuse" :
						if (pokemon.Ability=="OwnTempo") {
							_Status=="";
						}
						break;
				}
				pokemon.Status=_Status;
				if (pokemon.Ability=="Guts" && (pokemon.Status=="Burn" ||pokemon.Status=="Parlyz" ||pokemon.Status=="Poison" ||pokemon.Status=="Sleep")) {
					Attack+=pokemon.Attack;
				}
			}
			ReText();
		}
		//-------------------
		public function NotMiss(_value:Number):Boolean
		{
			var randomNumber:Number = Math.round(Math.random() * 100);
			if (randomNumber > _value)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		public function SendSQL(){
			var Query:String;
			if(CurrentMove==null)
				Query="UPDATE twalk_pokemon SET CurrentHP='"+pokemon.CurrentHP+"' ,Status='"+pokemon.Status+"' ,TemAttack='"+Attack+"' ,TemDefense='"+Defense+"' ,TemSpAtk='"+SpAtk+"' ,TemSpDef='"+SpDef+"' ,TemSpeed='"+Speed+"' ,PVPTurn='"+"0"+"' WHERE  upid='"+pokemon.upid+"'";
			else
				Query="UPDATE twalk_pokemon SET CurrentHP='"+pokemon.CurrentHP+"' ,Status='"+pokemon.Status+"' ,TemAttack='"+Attack+"' ,TemDefense='"+Defense+"' ,TemSpAtk='"+SpAtk+"' ,TemSpDef='"+SpDef+"' ,TemSpeed='"+Speed+"' ,MoveAttack='"+CurrentMove.Name+"' ,PVPTurn='"+PVPTurn+"' WHERE  upid='"+pokemon.upid+"'";
			//SQLData.SendQuery(Query);
			
		}
		public function LoadSQL(){
			//var Query:String="SELECT * FROM twalk_pokemon WHERE upid="+pokemon.upid;
			//SQLData.GetData("PVP",LoadData,Query);		
		}
		function LoadData(e:Event){
			var XMLData = new XML(e.target.data);
			pokemon.CurrentHP=XMLData.CurrentHP;
			pokemon.Status=XMLData.Status;
			Attack=XMLData.Attack;
			Defense=XMLData.Defense;
			SpAtk=XMLData.SpAtk;
			SpDef=XMLData.SpDef;
			Speed=XMLData.Speed;		
			if(PVPTurn!=XMLData.PVPTurn && IsAI){
				PVPTurn=XMLData.PVPTurn;
				AIMoveName=XMLData.MoveAttack;
				TargetLoadSQL();
			}
			ReText();	
		}
		public function TargetLoadSQL(){
			//var QueryTarget:String="SELECT * FROM twalk_pokemon WHERE upid="+Target.pokemon.upid;
			//SQLData.GetData("PVP",LoadTargetData,QueryTarget);
			
		}
		function LoadTargetData(e:Event){
			var XMLData = new XML(e.target.data);
					tweenList.unshift(new Tween (mcAtk,"alpha",Strong.easeOut,1,0,1,true));
					tweenList.unshift(new Tween (mcAtk,"x",Strong.easeOut,0,Target.x-this.x,1,true));
			Target.PVPGetAttack(XMLData,AIMoveName);			
		}
		function PVPGetAttack(XMLData:XML,moveName:String){
			ReceiveDamage(pokemon.CurrentHP-Number(XMLData.CurrentHP));
			pokemon.CurrentHP=XMLData.CurrentHP;
			pokemon.Status=XMLData.Status;
			Attack=XMLData.Attack;
			Defense=XMLData.Defense;
			SpAtk=XMLData.SpAtk;
			SpDef=XMLData.SpDef;
			Speed=XMLData.Speed;		
			ReText();				
		}
			
		public function AI()
		{
			IsAI=true;
		}
	}
}